﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace OALPTool
{
    public partial class DataGridViewEX :DataGridView
    {
        public DataGridViewEX()
        {
            InitializeComponent();
        }

        public DataGridViewEX(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        private string[] titleHead;
        [Browsable(true), DefaultValue(""), Description("列头数组"), Category("自定义"), EditorBrowsable(EditorBrowsableState.Advanced)]
        public string[] TitleHead
        {
            get
            {
                return this.titleHead;
            }
            set
            {
                this.titleHead = value;
            }
        }

        private string colHeaderText = "序号";
        private Color colHeaderTextColor = Color.Black;
        private Font colHeaderTextFont;
        private Color rowHeaderTextColor = Color.Black;
        private Font rowHeaderTextFont;
        private bool colHeaderTextVisible = true;
        private bool rowHeaderTextVisible = true;

        /// <summary>
        /// 设置颜色列表数据
        /// </summary>
        private ArrayList rowColorList = null;

        private bool isHeaderTextExis = false;

        [Browsable(true), DefaultValue(""), Description("列头的文本"), Category("自定义"), EditorBrowsable(EditorBrowsableState.Advanced)]
        public string HeaderText
        {
            get
            {
                return colHeaderText;
            }
            set
            {
                this.colHeaderText = value;
                if (!string.IsNullOrEmpty(value))
                {
                    this.colHeaderText = "序号";
                }

                this.Refresh();
            }
        }

        [Browsable(true), Description("列头的文本颜色"), Category("自定义"), EditorBrowsable(EditorBrowsableState.Advanced)]
        public Color HeaderTextColor
        {
            get
            {
                return this.colHeaderTextColor;
            }
            set
            {
                this.colHeaderTextColor = value;
                if (this.colHeaderTextColor.IsEmpty)
                {
                    this.colHeaderTextColor = this.ForeColor;
                }

                this.Refresh();
            }
        }

        [Browsable(true), Description("行头的文本颜色"), Category("自定义"), EditorBrowsable(EditorBrowsableState.Advanced)]
        public Color RowHeaderTextColor
        {
            get
            {
                return this.rowHeaderTextColor;
            }
            set
            {
                this.rowHeaderTextColor = value;
                if (this.rowHeaderTextColor.IsEmpty)
                {
                    this.rowHeaderTextColor = this.ForeColor;
                }

                this.Refresh();
            }
        }

        [Browsable(true), Description("列头的文本字体"), Category("自定义"), EditorBrowsable(EditorBrowsableState.Advanced)]
        public Font HeaderTextFont
        {
            get
            {
                if (this.colHeaderTextFont == null)
                {
                    this.colHeaderTextFont = this.Font;
                }
                return this.colHeaderTextFont;
            }
            set
            {
                if (value == null) this.colHeaderTextFont = this.Font;
                else this.colHeaderTextFont = value;

                this.Refresh();
            }
        }

        [Browsable(true), Description("行头的文本字体"), Category("自定义"), EditorBrowsable(EditorBrowsableState.Advanced)]
        public Font RowHeaderTextFont
        {
            get
            {
                if (this.rowHeaderTextFont == null)
                {
                    this.rowHeaderTextFont = this.Font;
                }

                return rowHeaderTextFont;
            }
            set
            {
                if (value == null) this.rowHeaderTextFont = this.Font;
                else this.rowHeaderTextFont = value;

                this.Refresh();
            }
        }

        [Browsable(true), Description("列头的文本是否要显示"), Category("自定义"), EditorBrowsable(EditorBrowsableState.Advanced)]
        public bool ColHeaderTextVisible
        {
            get
            {
                return this.colHeaderTextVisible;
            }

            set
            {
                this.colHeaderTextVisible = value;
                this.Refresh();
            }
        }

        [Browsable(true), Description("行头的文本是否要显示"), Category("自定义"), EditorBrowsable(EditorBrowsableState.Advanced)]
        public bool RowHeaderTextVisible
        {
            get
            {
                return this.rowHeaderTextVisible;
            }

            set
            {
                this.rowHeaderTextVisible = value;
                this.Refresh();
            }
        }

        /// <summary>
        /// ArrayList 是包函


        /// 设置行颜色的条件
        /// 参数是object数组 
        /// object对像包函
        ///       object[0] 绑定DataRowView数据的字段


        ///       object[1] 数据字段的值


        ///       object[2] 颜色 目前只支持 System.Drawing.Color 类型的颜色


        /// </summary>
        public ArrayList RowColorList
        {
            get
            {
                return this.rowColorList;
            }

            set
            {
                this.rowColorList = value;
            }
        }

        protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        {
            MultiHead m = new MultiHead(this, titleHead,HeaderTextFont);
            m.Draw(e);
        }

       protected override void OnRowPostPaint(System.Windows.Forms.DataGridViewRowPostPaintEventArgs e)
        {
            base.OnRowPostPaint(e);
            if (this.RowHeaderTextVisible == false) return;

            using (SolidBrush b = new SolidBrush(this.RowHeaderTextColor))
            {
                string str = (e.RowIndex + 1).ToString();

                StringFormat format = StringFormat.GenericDefault;
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;
                format.FormatFlags = StringFormatFlags.NoWrap;
                format.Trimming = StringTrimming.Character;

                int width = this.RowHeadersWidth;
                RectangleF rect = new RectangleF(e.RowBounds.Location.X + 1, e.RowBounds.Location.Y, width, e.RowBounds.Height);
                e.Graphics.DrawString(str, e.InheritedRowStyle.Font, b, rect, format);
            }

            if (rowColorList == null || rowColorList.Count == 0) return;

            SetRowColor(e);
        }

        protected override void OnDataSourceChanged(EventArgs e)
        {
            base.OnDataSourceChanged(e);

            setInitRowHeaderWidh();
        }

        protected override void OnDataMemberChanged(EventArgs e)
        {
            base.OnDataMemberChanged(e);

            setInitRowHeaderWidh();
        }

        protected override void OnColumnHeadersHeightChanged(EventArgs e)
        {
            //this.Refresh();
        }

        protected override void OnRowHeadersWidthChanged(EventArgs e)
        {
            //this.Refresh();
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{Tab}");
                e.SuppressKeyPress = true;
            }
            base.OnKeyDown(e);
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Enter)
            {
                keyData = Keys.Tab;
            }
            return base.ProcessDialogKey(keyData);
        }

        private void setInitRowHeaderWidh()
        {
            if (this.RowHeaderTextVisible == false) return;

            int Count = 0;
            if (this.DataSource.GetType().Name == "DataSet")
            {
                Count = ((DataSet)this.DataSource).Tables[this.DataMember].Rows.Count;
            }
            else if (this.DataSource.GetType().Name == "DataTable")
            {
                Count = ((DataTable)this.DataSource).Rows.Count;
            }
            else if (this.DataSource.GetType().Name == "DataView")
            {
                Count = ((DataView)this.DataSource).Count;
            }

            //设置行头和列头的大小
            System.Drawing.Graphics g = this.CreateGraphics();
            SizeF size1 = g.MeasureString(Count.ToString(), this.RowHeaderTextFont);
            SizeF size2 = g.MeasureString(this.HeaderText, this.HeaderTextFont);
            if (size1.Width < size2.Width) size1 = size2;
            g.Dispose();

            this.RowHeadersWidth = (int)Math.Ceiling(size1.Width) + 4;
        }

        private void SetRowColor(DataGridViewRowPostPaintEventArgs e)
        {
            for (int i = 0; i < rowColorList.Count; i++)
            {
                object[] Col = (object[])rowColorList[i];
                DataTable dtData = null;

                if (Col.Length == 0) continue;

                if (this.DataSource.GetType().Name == "DataSet")
                {
                    dtData = ((DataSet)this.DataSource).Tables[this.DataMember];
                }
                else if (this.DataSource.GetType().Name == "DataTable")
                {
                    dtData = ((DataTable)this.DataSource);
                }
                else if (this.DataSource.GetType().Name == "DataView")
                {
                    dtData = ((DataView)this.DataSource).Table;
                }

                string ColName = "", strWhere = "", strData = "";
                object ColorValue = null;

                try
                {
                    ColName = (string)Col[0];
                    strData = (string)Col[1];
                    ColorValue = (object)Col[2];
                }
                catch
                {
                    continue;
                }

                if (!dtData.Columns.Contains(ColName)) continue;

                DataGridViewRow dgvr = this.Rows[e.RowIndex];
                DataRow drCur = ((DataRowView)dgvr.DataBoundItem).Row;

                if (drCur[ColName].ToString() == strData)
                {
                    switch (ColorValue.GetType().Name)
                    {
                        case "Color":
                            dgvr.DefaultCellStyle.ForeColor = (Color)ColorValue;
                            dgvr.DefaultCellStyle.SelectionBackColor = (Color)ColorValue;
                            break;
                        default:
                            continue;
                    }
                }
            }
        }
    }
}
